/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Search.java
 *
 * Created on 22-Apr-2011, 12:07:01 PM
 */
package pos.ui.screen;

import java.awt.Toolkit;
import java.sql.SQLException;
import javax.swing.JTextField;
import pos.storage.db.mysql.SelectQueryTableModel;
import pos.storage.db.sql.SQL;

/**
 *
 * @author Anup
 */
public class Search extends javax.swing.JFrame {
    private SelectQueryTableModel searchTabelModel = new SelectQueryTableModel();
    private JTextField itemTextField = null;
    private Customer parentWindow;

    public Customer getParentWindow() {
        return parentWindow;
    }

    public void setParentWindow(Customer parentWindow) {
        this.parentWindow = parentWindow;
    }

    /** Creates new form Search */
    public Search() {
        initComponents();
        setIconImage(Toolkit.getDefaultToolkit().getImage("..\\pos\\images\\JavaCup.gif"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Search");
        setName("Search"); // NOI18N
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                onWindowClose(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Georgia", 1, 24));
        jLabel1.setText("Search Items");

        jTextField1.setFont(new java.awt.Font("Georgia", 0, 24));

        jLabel2.setFont(new java.awt.Font("Georgia", 0, 24)); // NOI18N
        jLabel2.setText("Name:");

        jLabel3.setFont(new java.awt.Font("Georgia", 0, 24));
        jLabel3.setText("Category:");

        jTextField2.setFont(new java.awt.Font("Georgia", 0, 24));

        jTable1.setAutoCreateRowSorter(true);
        jTable1.setFont(new java.awt.Font("Georgia", 0, 18));
        jTable1.setModel(searchTabelModel);
        jTable1.setToolTipText("Search Result");
        jTable1.setRowHeight(25);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        jButton1.setFont(new java.awt.Font("Georgia", 0, 24)); // NOI18N
        jButton1.setText("Select");
        jButton1.setToolTipText("Click this button to add the selected item to the invoice");
        jButton1.setEnabled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Georgia", 0, 24)); // NOI18N
        jButton2.setText("Search");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 751, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 731, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(276, 276, 276)
                .addComponent(jLabel1)
                .addContainerGap(313, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(310, 310, 310)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(267, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        searchTabelModel.removeAll();
        jTable1.setRowSelectionAllowed(true);
        String nameSearchCrit = jTextField1.getText().trim();
        String categorySearchCrit= jTextField2.getText().trim();
        String sqlQuery = "";
        if((nameSearchCrit.length() > 0) && (categorySearchCrit.length()==0)){
            sqlQuery = SQL.SEARCH_BY_NAME.replace("?", "\"%"+nameSearchCrit+"%\"");
        }else if((nameSearchCrit.length() == 0) && (categorySearchCrit.length() > 0)){
            sqlQuery = SQL.SEARCH_BY_CATEGORY.replace("?", "\"%"+categorySearchCrit+"%\"");
        }else{
            sqlQuery = SQL.SEARCH.replace("(?)", "\"%"+nameSearchCrit+"%\"");
            sqlQuery = SQL.SEARCH.replace("?", "\"%"+categorySearchCrit+"%\"");
        }
        
        try {
            searchTabelModel.genTable(pos.Pos.custConn, sqlQuery);
            if(searchTabelModel.getRowCount() > 0){
                jButton1.setEnabled(true);
            }else{
                jButton1.setEnabled(false);
            }
            
        } catch (SQLException ex) {
            
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int rowNumber = jTable1.getSelectedRow();
        String rowValue = (String) searchTabelModel.getValueAt(rowNumber, 0);
        JTextField textField = getItemTextField();
        textField.setText(rowValue);
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void onWindowClose(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_onWindowClose
        parentWindow.setVisible(true);
    }//GEN-LAST:event_onWindowClose
    
    public JTextField getItemTextField() {
        return itemTextField;
    }

    public void setItemTextField(JTextField itemTextField) {
        this.itemTextField = itemTextField;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
